<?php
namespace BI\Controller;
use Think\Controller;

class CommonController extends Controller{
    protected $LinkMode=0;//设置是否需要增加项目链接功能
    
    protected function _initialize(){
        
    }
    
    /**
     * @说明：此方法用F方法缓存金蛋数据【当数据更新或缓存不存在时，将自动更新缓存】
     * @param type $param
     * @return type
     */
    protected function getEggCacheFile($param){
        $name = serialize($param);
        $file = 'Bdata/Egg/'.md5($name);
      
        $data=F($file);
        if(empty($data)){
            set_time_limit(0);
            //$AnalyzeData = new \Common\Controller\BdataResultController($param);
            $AnalyzeData=new \Common\Controller\ReturnResultController($param);
            $result = $AnalyzeData->getAnalyzeDataResults();
         
            $sql = 'select top 1 id from mt_bdata_import_log order by id desc';
            $res = M()->query($sql);
            
            F($file,array('id'=>$res[0]['id'],'data'=>$result));
        }else{
            $sql = 'select top 1 id from mt_bdata_import_log order by id desc';
            $res = M()->query($sql);
       
            //数据表变动时
            if($data['id']==$res[0]['id']){
                
                set_time_limit(0);
                
                $AnalyzeData = new \Common\Controller\ReturnResultController($param);
                 
                $result = $AnalyzeData->getAnalyzeDataResults();
          
                F($file,array('id'=>$res[0]['id'],'data'=>$result));
            }else{
                $result = $data['data'];
            }
        }
        return $result;
    }
    
    
    /*
     * 用户行为监控
     * $data array
     * array(cid,sid,uid,module,date,times,ip,data,action);
     * cid : 企业ID
     * sid ：sessionID
     * uid ：用户ID
     * module ： 模块   1：home  2：eland  3：bdata 4：cmap
     * date : 时间格式，操作时间
     * times : 时间戳
     * ip : 操作地址
     * data : 提交数据
     * action : 操作类型  默认为1：查询
     */
    
     public function behavior($param){
        $data=array(
            'cid'=>$_SESSION['userInfo']['cid'],//$_SESSION['userInfo']['cid']
            'sid'=>session_id(),//session_id()
            'uid'=>$_SESSION['userInfo']['id'],//$_SESSION['userInfo']['id']
            'times'=>time(),
            'date'=>date('Y-m-d H:i:s',time()),
            'ip'  =>  get_client_ip(),
            'module'=>MODULE_NAME,
            'controller'=>CONTROLLER_NAME,
            'action'=>ACTION_NAME,
            'data'=>  json_encode($param)
        );
        $data['bdata']=$this->parseParam($param);
        $obj=D('Behavior');
        $obj->relation('bdata')->add($data); 
     }

     
     private function parseParam($param){
         $return['bType']= empty($param['type'])?0:$param['type'];
         $return['xkey']= implode(',', array_keys($param['xAxis']));
         $return['condtition']=serialize($param['where']);
         $return['ykey']=implode(',', array_keys($param['yAxis']));
         $return['vkey']= implode(',', array_keys($param['vAxis'])) ;
         $return['rkey']= $this->parseRanking($param['ranking']);
         $code = empty($param['code'])?'Bdata':$param['code'];
         $yAxis=array();$xAxis=array();
         
         $AnalyzeData=new \Common\Controller\BdataConfigController($code,1);
         $fieldType=$AnalyzeData->getAxisConfig();
         
         foreach($param['yAxis'] as $key=>$val){
             if((int)$fieldType[$key]['FieldType'] === 3){                 
                 $yAxis[]=$key.'|'.str_replace('|', ',',$val[0]);
             }
         }
         $return['yvalue']=  implode(';', $yAxis);
         foreach($param['xAxis'] as $key=>$val){
             if((int)$fieldType[$key]['FieldType'] === 3){                 
                 $xAxis[]=$key.'|'.str_replace('|', ',',$val[0]);
             }
         }
         $return['xvalue']=  implode(';', $xAxis);
         return $return;
     }
      
     /**
      * @解析排序字段
      * @param type $Ranking
      * @return type
      */
     private function parseRanking($Ranking){
         foreach ($Ranking as $key=>$val){
             if(is_array($val)){
                 $res[]=$key.':'.$val[0].'_'.$val[1];
             }else{
                 $res[]=$key.':'.$val;
             }             
         }
         return implode(',', $res);
     }

          /**
      * @处理二维数组的空值
      * @param type $array
      * @return type
      */
     public function dealArrayEmpty($array){
        foreach($array as $key=>$val){
            if(empty($val[0]) && empty($val[1])){
                unset($array[$key]);
            }
        }
        return $array;
     }
}
